import configparser
# 读取properties文件并返回一个字典
def read_properties(file_path):
    properties_dict = {}
    with open(file_path, 'r', encoding='utf-8') as file:
        for line in file:
            line = line.strip()
            # 忽略空行和注释行
            if line and not line.startswith('#'):
                key, value = line.split('=', 1)
                properties_dict[key] = value.strip()
    return properties_dict

# 根据参照文件的键的顺序，重新排列目标文件的键值对
def sort_properties_by_reference(reference_keys, target_dict):
    # 创建一个新的字典，用于存储排序后的键值对
    sorted_dict = {key: target_dict[key] for key in reference_keys if key in target_dict}
    return sorted_dict

# 将排序后的键值对写入新的properties文件
def write_properties(sorted_dict, output_file_path):
    with open(output_file_path, 'w', encoding='utf-8') as file:
        for key in sorted_dict:
            file.write(f"{key}={sorted_dict[key]}\n")

# 参照文件和目标文件的路径
reference_file_path = 'D:\Workspaces\study\Python\SimpleCode-Python\ota-portal-vsms-2024.04.08.8-RELEASE.properties'
target_file_path = 'D:\Workspaces\study\Python\SimpleCode-Python\ota-portal-vsms-cn.properties'
output_file_path = 'sorted_output.properties'

# 读取参照文件和目标文件
reference_keys = read_properties(reference_file_path).keys()
target_dict = read_properties(target_file_path)

# 排序目标文件的键值对
sorted_dict = sort_properties_by_reference(reference_keys, target_dict)

# 写入排序后的键值对到新文件
write_properties(sorted_dict, output_file_path)

print(f"Sorted properties have been written to {output_file_path}")